﻿using Jinndev.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using UnityEngine;

namespace Jinndev {

    /// <summary>
    /// Cocos组件
    /// </summary>
    public class CocosComponent {

        /// <summary>原始数据</summary>
        public JObject obj;

        /// <summary>组件类型</summary>
        public string type;

        /// <summary>所属节点</summary>
        public CocosNode node;

        public CocosComponent(JObject obj, string type) {
            this.obj = obj;
            this.type = type;
        }

        public bool HasSprite() {
            return type == "cc.Sprite" || type == "cc.Button" || type == "ParticleSystem2D";
        }

        public List<KeyValuePair<string, string>> GetSpriteUuids() {
            List<KeyValuePair<string, string>> uuids = new List<KeyValuePair<string, string>>();
            if (type == "cc.Sprite" || type == "cc.ParticleSystem2D") {
                FillUuid(uuids, "_spriteFrame.__uuid__");
            }
            else if (type == "cc.Button") {
                FillUuid(uuids, "_spriteFrame.__uuid__");
                FillUuid(uuids, "_normalSprite.__uuid__");
                FillUuid(uuids, "_pressedSprite.__uuid__");
                FillUuid(uuids, "_hoverSprite.__uuid__");
                FillUuid(uuids, "_disabledSprite.__uuid__");
            }
            return uuids;
        }

        private void FillUuid(List<KeyValuePair<string, string>> uuids, string path) {
            string uuid = JsonUtil.GetString(obj, path);
            if (!string.IsNullOrEmpty(uuid)) {
                path = CommonUtil.SubStringFromToLast(path, "_", ".__");
                uuid = CommonUtil.SubStringTo(uuid, "@");
                uuids.Add(new KeyValuePair<string, string>(path, uuid));
            }
        }

    }

}
